package ferp.android.views.table.animation;

import android.graphics.Point;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import ferp.android.views.CardView;
import ferp.android.views.table.TableView;
import ferp.android.views.table.element.trick.TrickView;

/**
* User: olegs
* Date: 04/01/2015 22:54
*/
public final class MovingCard
{
  public MovingCard(TableView.Listener listener, TableView table, TrickView trick)
  {
    this.listener = listener;
    this.table = table;
    this.trick = trick;
  }

  public void initialize(CardView cv, Point touch)
  {
    view = cv;
    // store view's origin
    origin.set(cv.getLeft(), cv.getTop());

    // calculate an offset from the touch point to left upper corner (used for adjustments during onMoving)
    if (touch != null)
      offset.set(touch.x - origin.x, touch.y - origin.y);
    else
      offset.set(0, 0);

    //Log.log.debug(Log.TAG, "initialized card " + cv.card() + ", enabled: " + cv.isEnabled()); // + origin: " + origin + ", touch: " + touch + ", offset: " + offset);
  }

  public void initialize(CardView cv) {view = cv;}

  public void move(int x, int y)
  {
    // calculate adjustment
    int left = x - offset.x;
    int top = y - offset.y;

    // layout the card within the current view borders
    if (left >= 0 && top >= 0 && left + view.getWidth() <= table.getWidth() && top + view.getHeight() <= table.getHeight())
    {
      view.layout(left, top);

      //Log.log.debug(Log.TAG, "move: " + new Point(left, top));
    }
  }

  public void commit(int player)
  {
    // parameters used on animation end
    // where to move the card
    CardView target = trick.getTargetCardView(player);
    // distance (take into account that the target positions are relative to their parent view group, i.e views.trick)
    int dx = target.getLeft() + trick.getLeft() - view.getLeft();
    int dy = target.getTop() + trick.getTop() - view.getTop();
    // animate movement from current position the target
    TranslateAnimation ta = new TranslateAnimation(0, dx, 0, dy);

    ta.setInterpolator(new DecelerateInterpolator());
    ta.setDuration(table.settings().animationTime);
    ta.setFillAfter(true);
    ta.setZAdjustment(Animation.ZORDER_TOP);
    ta.setAnimationListener(new AnimationCommitListener(listener, table, view, player, view.card()));

    // animate!
    view.setEnabled(false);
    view.startAnimation(ta);
  }

  public void rollback()
  {
    // this is to animate card moving back to its place
    int dx = origin.x - view.getLeft();
    int dy = origin.y - view.getTop();
    // animate movement from current position to original one
    TranslateAnimation ta = new TranslateAnimation(0, dx, 0, dy);

    //Log.log.debug(Log.TAG, "dx=" + dx + ", dy=" + dy);

    ta.setInterpolator(new DecelerateInterpolator());
    ta.setDuration(table.settings().animationTime);
    ta.setFillAfter(true);
    ta.setZAdjustment(Animation.ZORDER_TOP);
    ta.setAnimationListener(new AnimationRollbackListener(table, view, new Point(origin)));

    // animate!
    view.setEnabled(false);
    view.startAnimation(ta);
  }

  public void undo(CardView target)
  {
    // this is to animate card moving back to its place
    int dx = view.getLeft() + trick.getLeft() - target.getLeft();
    int dy = view.getTop() + trick.getTop() - target.getTop();
    // animate movement from current position the target
    TranslateAnimation ta = new TranslateAnimation(dx, origin.x - target.getLeft(), dy, origin.y - target.getTop());

    ta.setInterpolator(new DecelerateInterpolator());
    ta.setDuration(table.settings().animationTime);
    ta.setFillAfter(true);
    ta.setZAdjustment(Animation.ZORDER_TOP);
    ta.setAnimationListener(new AnimationUndoListener(table, target));

    // animate!
    view.setVisibility(View.INVISIBLE);

    target.setEnabled(false);
    target.startAnimation(ta);
  }

  private TableView.Listener listener;
  private TableView table;
  private TrickView trick;

  // moving card view
  private CardView view;
  // moving card view origin point
  private final Point origin = new Point();
  // touch point shift from upper left corner of the card view
  private final Point offset = new Point();
}
